package com.gdit.zax.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.dao.FindPwdDao;
import com.gdit.zax.entitys.FindPwd;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.LogUtil;

public class FindPwdDaoImpl implements FindPwdDao {
	private PreparedStatement ps;
	private ResultSet rs;
	
	private Connection conn;
	private HttpServletRequest request;
	private HttpServletResponse response;
	
	
	public FindPwdDaoImpl(Connection conn,HttpServletRequest request, HttpServletResponse response) {
		this.conn = conn;
		this.request = request;
		this.response = response;
	}
	
	public boolean addFindPwd(FindPwd data) {
		String sql = "insert t_findpwd set UserId=?,UserName=?,Captche=?,Type=?,ChangeFlag=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, data.getUserid());
			ps.setString(2, data.getUsername());
			ps.setString(3, data.getCaptche());
			ps.setInt(4, data.getType());
			ps.setString(5, data.getChangeflag());
			
			int result = ps.executeUpdate();
			if (result!=0) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean delFindPwd(int fid) {
		// TODO Auto-generated method stub
		return false;
	}

	public FindPwd getFindPwd(int uid) {
		// TODO Auto-generated method stub
		return null;
	}

	public FindPwd getFindPwd(String changeflag) {
		String sql = "select * from t_findpwd where ChangeFlag=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1,changeflag);
			
			rs = ps.executeQuery();
			if (rs.next()) {
				return DBUtil.getobject(rs, FindPwd.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public ArrayList<FindPwd> listFindPwd(int uid) {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean changePwd(int uid, String changeflag, String pwd) {
//		String sql = "update t_user set Accesstoken=?,logintime=now() where Id=?";	
		FindPwd info = getFindPwd(changeflag);
		if (info==null) {
			return false;
		}
		if (info.getUserid()!=uid) {
			return false;
		}
		
		String sql = "update t_user set Password=? where Id=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1,pwd);
			ps.setInt(2, info.getUserid());
			
			int resu =  ps.executeUpdate();
			if (resu!=0) {
				sql = "DELETE from t_findpwd where id=?";
				ps = conn.prepareStatement(sql);
				ps.setInt(1,info.getId());
				
				resu =  ps.executeUpdate();
				if (resu!=0) {
					return true;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean changePwd(String username, String changeflag, String pwd) {
		FindPwd info = getFindPwd(changeflag);
		if (info==null) {
			return false;
		}
		if (username==null) {
			return false;
		}
		if (!username.equals(info.getUsername())) {
			return false;
		}
		
		String sql = "update t_user set Password=? where Id=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1,pwd);
			ps.setInt(2, info.getUserid());
			
			int resu =  ps.executeUpdate();
			if (resu!=0) {
				sql = "DELETE from t_findpwd where id=?";
				ps = conn.prepareStatement(sql);
				ps.setInt(1,info.getId());
				
				resu =  ps.executeUpdate();
				if (resu!=0) {
					return true;					
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

}
